package com.omesoft.hksnore.util.omeview;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Rect;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import java.util.Random;

/**
* 录音绘图
* @autor omesoft_lgc 
*
* create at 2017/5/17 20:58
*
*/
@SuppressLint("DrawAllocation")
public class RecordPragramBackUp extends View {

    private Paint paint;
    private Random random;// 随机数，用于随机生成条柱的高度
    private int width_Block = 10;// 用于控制小模块以及条柱的宽度
    private int height_Block = 10;// 小方块的高度
    private boolean canDraw = false;// 控制是否绘图
    private int width_Canvas, height_Canvas;

    private int length = 2048;//
    private int columnCount = length;// 整个画布中，条柱的条数
    private int point_X[] = new int[length];
    private int point_Y[] = new int[length];
    private int point_x[] = new int[length];
    private int point_y[] = new int[length];
    private Rect rect_Block[] = new Rect[length];// 小方块矩形框
    private Rect rect_Column[] = new Rect[length];// 条柱矩形框

    int eatchWidth = 30;
    float max = 128.0f;

    public RecordPragramBackUp(Context context) {
        super(context);
    }

    public RecordPragramBackUp(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @SuppressLint("DrawAllocation")
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        height_Canvas = getMeasuredHeight();
        width_Canvas = getMeasuredWidth();
        height_Canvas = height_Canvas - 100;
        initPaint();
    }

    public void init(int _length) {
        this.length = _length;
        columnCount = length;// 整个画布中，条柱的条数
        eatchWidth = width_Canvas / 64;
        point_X = new int[length];
        point_Y = new int[length];
        point_x = new int[length];
        point_y = new int[length];
        rect_Block = new Rect[length];// 小方块矩形框
        rect_Column = new Rect[length];// 条柱矩形框

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (canDraw) {
            for (int i = 0; i < columnCount; i++) {
                canvas.drawRect(rect_Column[i], paint);// 绘制条柱
//				canvas.drawRect(rect_Block[i], paint);// 绘制小方块
            }
            Log.e("xx", "刷新结束。。");
        }
        // 画竖线
        canvas.drawLine(0, 0, 0, height_Canvas, paint);
        // 画横坐标线
        canvas.drawLine(0, height_Canvas, width_Canvas, height_Canvas, paint);
        canvas.drawText("snoreCount:" + snore, 50, 50, paint);
    }

    public static int getMax(int[] arr) {
        int max = arr[0];

        for (int x = 1; x < arr.length; x++) {
            if (arr[x] > max)
                max = arr[x];

        }
        return max;

    }

    int[] outInt;
    int snore = 0;

    public void update(boolean canDraw, int[] outInt,  int _snore) {
        synchronized (outInt) {
            if (outInt.length == 0) {
                return;
            }
            this.snore = _snore;
            try {
                columnCount = outInt.length;
                this.canDraw = canDraw;
                this.outInt = outInt;
                String str = "";
                String str2 = "";
                Log.e("xx", "----------outInt.length-------- " + outInt.length);

                int max1 = getMax(outInt);

                int startX = 0;
                int startY = 0;
                int stopX = 0;
                int stopY = 0;

                Log.e("xx", outInt.length + "----------开始添加数据------- " + columnCount);
                for (int i = 0; i < columnCount; i++) {
                    point_X[i] = eatchWidth * (i + 1);

                    int rh = (int) ((float) (outInt[i] / max) * height_Canvas);
                    str2 += outInt[i] + ",";

                    startX = point_X[i];
                    startY = height_Canvas - rh;

                    stopX = point_X[i] + width_Block * 2;
                    stopY = height_Canvas;

                    point_Y[i] = startY;

                    rect_Column[i] = new Rect(startX, startY, stopX, stopY);

                    str += startY + ",";


                    // 判断条柱上次高度与当前本次高度的高度值如若本次高度未达到上次高度则小方块的高度逐渐，实现下落的效果
//					point_x[i] = point_X[i];
//					if (point_y[i] > point_Y[i] - 5) {
//						point_y[i] = point_y[i] - 5;
//					} else {
//						point_y[i] = point_Y[i];
//					}
//					rect_Block[i] = new Rect(point_x[i] - width_Block / 2, height_Canvas - point_y[i], point_x[i] + width_Block*2, height_Canvas - point_y[i] + 10);


                }
                Log.e("xx", "----------添加成功 -------- ");
                handler.sendEmptyMessage(0);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    Handler handler = new Handler() {
        public void dispatchMessage(android.os.Message msg) {

            invalidate();

        }

        ;
    };

	/*
     * 初始化所有条柱的宽、高信息
	 */
    // private void intiRectColumn() {
    // random = new Random();
    // for (int i = 0; i < columnCount; i++) {
    // point_X[i] = width * (i + 1);
    // point_Y[i] = random.nextInt((height_Canvas - 20));
    // rect_Column[i] = new Rect(point_X[i] - width_Block / 2, height_Canvas -
    // point_Y[i], point_X[i] + width_Block / 2, height_Canvas);
    //
    // }
    // }

    /*
     * 初始化所有小方块的宽、高信息
     */
    private void intiRectBlock() {
        for (int i = 0; i < columnCount; i++) {
            point_x[i] = point_X[i];
            // 判断条柱上次高度与当前本次高度的高度值如若本次高度未达到上次高度则小方块的高度逐渐，实现下落的效果
            if (point_y[i] > point_Y[i] - height_Block) {
                point_y[i] = point_y[i] - 10;
            } else {
                point_y[i] = point_Y[i];
            }
            rect_Block[i] = new Rect(point_x[i] - width_Block / 2, height_Canvas - point_y[i], point_x[i] + width_Block / 2, height_Canvas - point_y[i] + 10);
        }
    }

    /*
     * 初始化画笔Paint
     */
    private void initPaint() {
        paint = new Paint();
        paint.setDither(true);// 去抖动
        paint.setAntiAlias(true);// 抗锯齿
        paint.setStyle(Style.FILL);// 设置填充模式
        paint.setColor(Color.rgb(0, 128, 255));
        paint.setTextSize(60);
        // paint.setColor(Color.WHITE);
		/*
		 * 设置条柱的颜色.注意，这个Demo因为有背景图片所以设置白色条柱能够看得见， 当背景颜色为白色时，记得更改条柱颜色
		 */
    }

    public boolean isCanDraw() {
        return canDraw;
    }

    public void setCanDraw(boolean canDraw) {
        this.canDraw = canDraw;
    }

}

